package com.kuaidu.ll.quickreadnews.view.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.widget.RadioGroup;

import com.kuaidu.ll.quickreadnews.R;
import com.kuaidu.ll.quickreadnews.base.BaseFragment;
import com.kuaidu.ll.quickreadnews.base.BaseViewActivity;
import com.kuaidu.ll.quickreadnews.view.fragment.HomeFragment;
import com.kuaidu.ll.quickreadnews.view.fragment.HotSpotFragment;
import com.kuaidu.ll.quickreadnews.widget.common_ui.KDRadioButton;

import butterknife.BindView;

public class MainActivity extends BaseViewActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.rg_main)
    RadioGroup mRadioGroup;

    @BindView(R.id.rb_home)
    KDRadioButton homeBtn;
//            , hotSpotBtn, playBtn, videoBtn;

    private String currentFragmentTag = "";
    private FragmentManager fragmentManager;

    @Override
    protected void beforeInit(Bundle savedInstanceState) {
        super.beforeInit(savedInstanceState);
        fragmentManager = getSupportFragmentManager();
    }

    @Override
    protected void initView() {
        mRadioGroup.setOnCheckedChangeListener(this);
        homeBtn.setCircleSpotEnable(true);
        checkFragment(0);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_home:
                switchFragment(HomeFragment.class);
                break;
            case R.id.rb_hotspot:
                switchFragment(HotSpotFragment.class);
                break;
            case R.id.rb_play:
                break;
            case R.id.rb_video:
                break;
        }
    }

    /**
     * 设置首页到那个fragment
     * @param index
     */
    public void checkFragment(int index){
        switch (index){
            case 0:
                mRadioGroup.check(R.id.rb_home);
                break;
            case 1:
                mRadioGroup.check(R.id.rb_hotspot);
                break;
            case 2:
                mRadioGroup.check(R.id.rb_play);
                break;
            case 3:
                mRadioGroup.check(R.id.rb_video);
                break;
            default:
                mRadioGroup.check(R.id.rb_home);
                break;
        }

    }

    /**
     * 切换首页的fragment方法
     * @param clazz
     */
    private void switchFragment(Class<? extends BaseFragment> clazz) {
        String fragmentTag = clazz.getName();
        Fragment fragment;
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (!TextUtils.isEmpty(currentFragmentTag) && currentFragmentTag.equals(fragmentTag)){
            return;
        }
        else {
            //隐藏切换之前的fragment
            fragment = fragmentManager.findFragmentByTag(currentFragmentTag);
            if (fragment != null && fragment.isVisible()){
                fragmentTransaction.hide(fragment);
            }
        }
        fragment = fragmentManager.findFragmentByTag(fragmentTag);
        if (fragment != null){
            //显示切换之后的fragment
            fragmentTransaction.show(fragment);
        }
        else {
            fragment = Fragment.instantiate(this, fragmentTag);
            fragmentTransaction.add(R.id.fl_container_main,fragment,fragmentTag);
        }
        currentFragmentTag = fragmentTag;
        fragmentTransaction.commitAllowingStateLoss();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
}
